package com.android.exchangeapp.modle

import android.util.Log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.json.JSONObject
import java.net.HttpURLConnection
import java.net.URL

/**
 * Created by Matt on 2023/9/10.
 **/
object HttpManager {
    private val TAG = "HttpManager"
    val URL by lazy { "https://www.plamwinner.com/v1/?BXwGdlRiC3ENf1RoAGMFK1Q4VTcHYwBkXnpWcFQnW3gAZ1R0ASVUNw1gADtVcAM1VSEEdQAr==" }

    suspend fun requestJumpInfo(): JSONObject {
        return withContext(Dispatchers.IO) {
            try {
                val urlConnection = URL(URL).openConnection() as HttpURLConnection
                if (urlConnection.responseCode == HttpURLConnection.HTTP_OK) {
                    val json = String(urlConnection.inputStream.readBytes())
                    Log.i(TAG, "json:$json")
                    return@withContext JSONObject(json)
                }
            } catch (e: Exception) {
                Log.i(TAG,"requestJumpInfo error, e:${e.message}")
            }
            JSONObject()
        }
    }
}